import { AnimationName, AudioType, CardColor, CardType, CurrentPlayer, ListenEventType, PanelType, playerPeople, PopupType } from "../../util/defineConfig";
import { nodeTouchStart } from "../../util/utilBase";
import View from "../../util/View";
import { Card } from "../gameModel";
import GameView from "../gameView";

const { ccclass, property } = cc._decorator;

@ccclass
export default class gamePanel extends View {
  private gameView: GameView = null
  private playerP: playerPeople = playerPeople.twoP
  private view: any = {}

  private player1PCards: cc.Node[] = []
  private player2PCards: cc.Node[] = []
  private player3PCards: cc.Node[] = []
  private player4PCards: cc.Node[] = []

  private holeCards: cc.Node[] = []

  private drapCard: cc.Node[] = []
  private time: number = 0     //发牌速度

  private allCard: cc.Node[] = []

  private specialCard: Card = null

  init(gameView: GameView, index: playerPeople) {
    this.gameView = gameView
    this.playerP = index
    this.time = 0.3 - (this.playerP / 15)
    this.emit(ListenEventType.POST_MODEL_PLAYERNUM, index)
  }
  onLoad() {
    this.view = this.load_all_object(this.node, '')
    //  this.restartGame()
    nodeTouchStart(this.view['but_uno'], this.unoClickEvent.bind(this), this)
    nodeTouchStart(this.view['but_exit'], this.showRestartPanel.bind(this), this)

    this.show_ui_at(this.node, PanelType.POPUPPANEL, this, false, ListenEventType.GET_POPUPPANEL_BY_VIEW, PopupType.explain)
  }

  public initCard() {
    this.gameView.UICards.forEach((item) => {
      item.parent = this.view['dealCardArea']
      this.allCard.push(item)
    })
    this.initScene()
  }

  private unoClickEvent() {
    this.emit(ListenEventType.POST_MODEL_ISUNO)
  }

  private showRestartPanel() {
    this.emit(ListenEventType.PLAY_EFFECT, AudioType.press_button)
    this.show_ui_at(this.node, PanelType.POPUPPANEL, this, false, null, PopupType.selectRestart)
  }

  // 初始化场景
  private initScene() {
    this.view['player1'].active = true

    this.emit(ListenEventType.MODEL_DEAL_CAEDS)
    switch (this.playerP) {
      case playerPeople.twoP: this.player2PHandle(); break;
      case playerPeople.threeP: this.player3PHandle(); break;
      case playerPeople.fourP: this.player4PHandle(); break;
    }
    for (let i = 0; i < 4; i++) {
      this.view[`player${i + 1}/bar`].opacity = 100
    }
  }

  // 2P
  private player2PHandle() {
    this.view['player2'].active = false
    this.view['player3'].active = true
    this.view['player4'].active = false
    this.view['player3/bar/tip'].getComponent(cc.Label).string = 'PLAYER2'
  }

  // 3P
  private player3PHandle() {
    this.view['player2'].active = true
    this.view['player3'].active = false
    this.view['player4'].active = true
    this.view['player4/bar/tip'].getComponent(cc.Label).string = 'PLAYER3'

  }

  // 4P
  private player4PHandle() {
    this.view['player2'].active = true
    this.view['player3'].active = true
    this.view['player4'].active = true
  }

  // 发牌
  public dealCards(card: Card, player: CurrentPlayer, index: number, insertIndex: number = 0) {
    if (player === CurrentPlayer.Hole) {
      this.dealCard2HoleCardArea(card, player, index, insertIndex)
      return
    }
    switch (this.playerP) {
      case playerPeople.twoP:
        this.deal2PCard(card, player, index);
        break;
      case playerPeople.threeP:
        this.deal3PCard(card, player, index, insertIndex);
        break;
      case playerPeople.fourP:
        this.deal4PCard(card, player, index);
        break;
    }
  }


  // 移动牌区
  private moveCard(cards: cc.Node[], player: CurrentPlayer) {
    this.handleCardPos(cards, player)
    let px = 0
    if (this.playerP === playerPeople.threeP) {
      if (player === CurrentPlayer.Player1) {
        px = -78
      } else {
        px = -48.4
      }
    }
    else if (this.playerP === playerPeople.fourP) {
      if (player === CurrentPlayer.Player1 || player === CurrentPlayer.Player3) {
        px = -78
      } else {
        px = -48.4
      }
    }
    else {
      px = -78
    }
    // cards.forEach((node) => {
    //   cc.tween(node).by(0.3, { x: px }).start()
    // })
  }

  // 处理牌的位置
  private handleCardPos(playArea: cc.Node[], player: CurrentPlayer) {
    let pos = 0
    let px = 0
    if (this.playerP === playerPeople.threeP) {
      if (player === CurrentPlayer.Player1) {
        pos = ((playArea.length - 1) * 78 + 156) / 2
        px = 78
      } else {
        pos = ((playArea.length - 1) * 48.4 + 156) / 2
        px = 48.4
      }
    }
    else if (this.playerP === playerPeople.fourP) {
      if (player === CurrentPlayer.Player1 || player === CurrentPlayer.Player3) {
        pos = ((playArea.length - 1) * 78 + 156) / 2
        px = 78
      } else {
        pos = ((playArea.length - 1) * 48.4 + 156) / 2
        px = 48.4
      }
    }
    else {
      pos = ((playArea.length - 1) * 78 + 156) / 2
      px = 78
    }

    playArea.forEach((item, index) => {
      item.zIndex = index
      cc.tween(item).to(0.1, { x: -pos + index * px }).start()
    })
  }

  // 出完牌后要进行牌区的位置补进
  private moveCardFromLastPos(card: cc.Node, player: CurrentPlayer) {
    let loc = null
    let playerCards: cc.Node[] = this.getPlayerCards(player)

    playerCards.some((node, index) => {
      if (node === card) {
        loc = index
        return true
      }
    })
    if (loc === null) {
      console.error('出牌错误')
      return
    }
    playerCards.splice(loc, 1)
    let cards: cc.Node[] = []
    for (let i = loc; i < playerCards.length; i++) {
      cards.push(playerCards[i])
    }
    this.moveCard(cards, player)
  }

  // 发牌区翻牌到回收区
  public openSendAreaToRecycleArea(card: Card) {
    let node = card.UICard.node
    this.move(node, this.view['drapCardArea'])
    this.drapCard.push(node)
    cc.tween(node).delay(this.time * 7 * (2 + this.playerP) + this.time).to(0.3, { position: cc.v3(0, 0, 0) }).call(() => {
      this.emit(ListenEventType.PLAY_EFFECT, AudioType.card)
      cc.tween(node).to(0.3, { scaleX: 0 }).call(() => { card.flopCard() }).to(0.3, { scaleX: 1 }).start()
      cc.tween(this.view['drapCardArea']).by(0.3, { x: -78 }).start()
      cc.tween(this.view['dealCardArea']).by(0.3, { x: -78 }).start()
      this.setGameState(card)
    }).start()
  }

  // 设置场景状态
  private setGameState(card: Card) {
    if (card.type !== CardType.number) {
      this.handleCardEff(card)
    }
    this.setBtnColorState(card.color)
  }

  public setBtnColorState(color: CardColor) {
    this.view['card_color/_oButBlue'].active = false
    this.view['card_color/_oButGreen'].active = false
    this.view['card_color/_oButYellow'].active = false
    this.view['card_color/_oButRed'].active = false
    switch (color) {
      case CardColor.blue: this.view['card_color/_oButBlue'].active = true; break;
      case CardColor.green: this.view['card_color/_oButGreen'].active = true; break;
      case CardColor.yellow: this.view['card_color/_oButYellow'].active = true; break;
      case CardColor.red: this.view['card_color/_oButRed'].active = true; break;
    }
  }

  public setBarState(player: CurrentPlayer) {
    let arr = this.returnAllPlayer()
    arr.forEach((item) => {
      this.view[`${item}/bar`].opacity = 100
    })
    this.view[`${arr[player]}/bar`].opacity = 255
  }

  private returnAllPlayer(): Array<string> {
    switch (this.playerP) {
      case playerPeople.twoP: return ['player1', 'player3']; break;
      case playerPeople.threeP: return ['player1', 'player2', 'player4']; break;
      case playerPeople.fourP: return ['player1', 'player2', 'player3', 'player4']; break;
    }
  }

  // 出牌（1P）
  public recycleCard(card: Card, player: CurrentPlayer) {
    let node = card.UICard.node
    let drapArea = this.view['drapCardArea']
    this.move(node, drapArea, false)
    node.zIndex = this.drapCard[this.drapCard.length - 1].zIndex + 1
    this.drapCard.push(node)
    cc.tween(node).to(0.5, { position: cc.v3(0, -node.height, 0) }).call(() => {
      //  出牌后的响应操作
      this.emit(ListenEventType.PLAY_EFFECT, AudioType.card)
      this.moveCardFromLastPos(node, player)
      this.scheduleOnce(() => { this.handleCardPos(this.getPlayerCards(player), player) }, 0.3)
      this.emit(ListenEventType.POST_CARD_COLOR, card)
      /**
       * 中间需要对卡牌的效果进行判断
       */
      if (card.type === CardType.number) {
        this.scheduleOnce(() => { 
          this.emit(ListenEventType.NEXT_PLAYER, card) 
          this.getPlayerCards(player).forEach(node => {
            node.color = cc.Color.WHITE
          });
        }, 0.5)
      } else {
        this.handleCardEff(card)
      }
    }).start()
  }

  // 选中卡牌
  public selectCard(card: Card, player: CurrentPlayer) {
    if (!card.canClick) return

    let node = card.UICard.node
    // let drapArea = this.view['drapCardArea']
    // this.move(node, drapArea, false)
    // node.zIndex = this.drapCard[this.drapCard.length - 1].zIndex + 1
    // this.drapCard.push(node)
    let position = node.position
    if (card.UICard.isSelected()) {
      position.y -= node.height / 4
    } else {
      position.y += node.height / 4
    }
    if (card.UICard.isSelected()) {
      card.selected = false
    } else {
      card.selected = true
    }
    
    card.UICard.setSelected(!card.UICard.isSelected())
    card.canClick = false
    cc.tween(node).to(0.5, { position: position }).call(() => {
      //  出牌后的响应操作
      this.emit(ListenEventType.PLAY_EFFECT, AudioType.card)
      card.canClick = true
      // this.moveCardFromLastPos(node, player)
      // this.scheduleOnce(() => { this.handleCardPos(this.getPlayerCards(player), player) }, 0.3)
      // this.emit(ListenEventType.POST_CARD_COLOR, card)
      /**
       * 中间需要对卡牌的效果进行判断
       */
      // if (card.type === CardType.number) {
      //   this.scheduleOnce(() => { this.emit(ListenEventType.NEXT_PLAYER, card) }, 0.5)
      // } else {
      //   this.handleCardEff(card)
      // }
    }).start()
  }

  // 处理特殊卡牌效果
  private handleCardEff(card: Card) {
    if (card.type === CardType.number) return
    this.emit(ListenEventType.POST_MODEL_ISGAMEOVER)
    this.specialCard = card
  }

  // 处理特殊牌前先判断是否游戏结束
  public handleIsGameOver(isGameOver: boolean) {
    if (!isGameOver) {
      this.scheduleOnce(() => {
        switch (this.specialCard.type) {
          case CardType.add_2:
            this.addEffect(AnimationName.draw_2, CardType.add_2);
            this.emit(ListenEventType.PLAY_EFFECT, AudioType.special_card); break;
          case CardType.add_4:
            this.addEffect(AnimationName.draw_4, CardType.add_4);
            this.emit(ListenEventType.PLAY_EFFECT, AudioType.special_card); break;
          case CardType.ban:
            this.addEffect(AnimationName.stop_turn, CardType.ban);
            this.emit(ListenEventType.PLAY_EFFECT, AudioType.special_card); break;
          case CardType.changeColor:
            this.addEffect(AnimationName.all_Color, CardType.changeColor);
            this.emit(ListenEventType.PLAY_EFFECT, AudioType.change_color);
            break;
          case CardType.turn:
            this.addEffect(AnimationName.change_clockwise, CardType.turn);
            this.emit(ListenEventType.PLAY_EFFECT, AudioType.special_card);
            break;
          default: break;
        }
      }, 0.1)
    }
  }

  // 人机出牌（非1P）
  public robotDealCard(card: Card, player: CurrentPlayer) {
    let node = card.UICard.node
    node.angle = 0
    this.move(node, this.view['drapCardArea'])
    node.zIndex = this.drapCard[this.drapCard.length - 1].zIndex + 1
    this.drapCard.push(node)
    let py = node.y / 2

    // 出牌的x位置
    let xPos = 0, yPos = 0
    let multi = 2
    switch (player) {
      case CurrentPlayer.Player2: xPos = -node.width; break;
      case CurrentPlayer.Player3: {
        if (this.playerP === playerPeople.threeP) {
          xPos = node.width;
        }
        if (this.playerP === playerPeople.fourP) {
          yPos = node.height;
        }
        break
      }
      case CurrentPlayer.Player4: xPos = node.width;; break;
      default: break;
    }
    xPos *= multi
    yPos *= multi
    
    // if (player === CurrentPlayer.Player2) xPos = -node.width

    cc.tween(node)
      .to(0.2, { position: cc.v3(0, py, 0), scaleX: 0 })
      .call(() => {
        card.flopCard()
      })
      .to(0.2, { position: cc.v3(xPos, yPos, 0), scaleX: 1 })
      .call(() => {
        this.emit(ListenEventType.PLAY_EFFECT, AudioType.card)
        this.moveCardFromLastPos(node, player)
        this.scheduleOnce(() => { this.handleCardPos(this.getPlayerCards(player), player) }, 0.3)
        this.emit(ListenEventType.POST_CARD_COLOR, card)
        if (card.type === CardType.number) {
          this.scheduleOnce(() => { this.emit(ListenEventType.NEXT_PLAYER, card) }, 0.5)
        } else {
          this.handleCardEff(card)
        }
      })
      .start()
  }


  // 获取对应区域卡牌
  private getPlayerCards(player: CurrentPlayer): Array<cc.Node> {
    let playerCards: cc.Node[] = []
    switch (player) {
      case CurrentPlayer.Player1: playerCards = this.player1PCards; break;
      case CurrentPlayer.Player2: playerCards = this.player2PCards; break;
      case CurrentPlayer.Player3: playerCards = this.player3PCards; break;
      case CurrentPlayer.Player4: playerCards = this.player4PCards; break;

    }
    return playerCards
  }

  // 将牌移入其他区域
  private move(node: cc.Node, parent: cc.Node, cleanup: boolean = false) {
    let wp = node.convertToWorldSpaceAR(cc.v3(0, 0, 0))
    let np = parent.convertToNodeSpaceAR(wp)
    node.position = np
    node.removeFromParent(cleanup)
    parent.addChild(node)
  }

  // 添加卡牌效果动画
  private addEffect(aniamtion: string, type: CardType) {
    this.show_ui_at(this.node, aniamtion, this, true, null, type, aniamtion)
  }

  // 发牌区发牌到玩牌区(发牌时)2P
  private deal2PCard(card: Card, player: CurrentPlayer, index: number) {
    let node = card.UICard.node
    let obj = this.getPlayerAreaByPlayerP(player)
    let playArea: cc.Node = obj.playArea
    let playerCard: cc.Node[] = obj.playerCard
    this.move(node, playArea, false)
    let count = playArea.childrenCount
    let px = 0
    if (count >= 4) {
      px = (count - 4) * 78
    }
    cc.tween(node)
      .delay(index * this.time)
      .to(0.3, { position: cc.v3(px, 0, 0) })
      .call(() => {
        playerCard.push(node)
        this.emit(ListenEventType.PLAY_EFFECT, AudioType.card_dealing)
        if (player === CurrentPlayer.Player1) {
          cc.tween(node)
            .to(0.3, { scaleX: 0 })
            .call(() => {
              card.flopCard()
            })
            .to(0.3, { scaleX: 1 })
            .start()
        }
        if (count < 4) {
          this.moveCard(playerCard, player)
        }
      })
      .start()
  }

  // 发牌区发牌到玩牌区(发牌时)3P
  private deal3PCard(card: Card, player: CurrentPlayer, index: number, insertIndex: number) {
    let node = card.UICard.node
    let obj = this.getPlayerAreaByPlayerP(player)
    let playArea: cc.Node = obj.playArea
    let playerCard: cc.Node[] = obj.playerCard
    if (player !== CurrentPlayer.Player1) node.angle = 90
    this.move(node, playArea, false)
    let count = playArea.childrenCount
    let px = 0
    if (player === CurrentPlayer.Player1) {
      // if (count >= 4) {
        px = 78
      // }
    }
    else {
      if (count >= 4) {
        px = (count - 4) * 48.4
      }
    }
    if (player === CurrentPlayer.Player1) {
      cc.tween(node)
        .delay(index * this.time)
        .to(0.3, { position: cc.v3(px, 0, 0) })
        .call(() => {
          // playerCard.push(node)
          // playerCard.unshift(node)
          // console.log('count', Math.round(count / 2))
          // let playerCardModel = getar
          // console.log('insertIndex = ', insertIndex)
          // console.log("card", card.color, card.index)
          playerCard.splice(insertIndex, 0, node)
          // playerCard.map((item, index) => {
          //   console.log(index, item.x)
          // })
          // this.getPlayerCards(player).map((item, index) => {

          
          this.emit(ListenEventType.PLAY_EFFECT, AudioType.card_dealing)
          cc.tween(node)
            .to(0.3, { scaleX: 0 })
            .call(() => {
              card.flopCard()
            })
            .to(0.3, { scaleX: 1 })
            .start()
          // if (count < 4) {
            
            this.moveCard(playerCard, player)
          // }
        }).start()
    }
    else {
      cc.tween(node)
        .delay(index * this.time)
        .to(0.3, { position: cc.v3(px, 0, 0) })
        .call(() => {
          playerCard.push(node)
          this.emit(ListenEventType.PLAY_EFFECT, AudioType.card_dealing)
          if (count < 4) {
            this.moveCard(playerCard, player)
          }
        }).start()
    }

  }
  // 发牌区发牌到玩牌区(发牌时)4P
  private deal4PCard(card: Card, player: CurrentPlayer, index: number) {
    let node = card.UICard.node
    let obj = this.getPlayerAreaByPlayerP(player)
    let playArea: cc.Node = obj.playArea
    let playerCard: cc.Node[] = obj.playerCard
    if (player !== CurrentPlayer.Player1 && player !== CurrentPlayer.Player3) node.angle = 90
    this.move(node, playArea, false)

    let count = playArea.childrenCount
    let px = 0
    if (player === CurrentPlayer.Player1 || player === CurrentPlayer.Player3) {
      if (count >= 4) {
        px = (count - 4) * 78
      }
    }
    else {
      if (count >= 4) {
        px = (count - 4) * 48.4
      }
    }
    if (player === CurrentPlayer.Player1) {
      cc.tween(node)
        .delay(index * this.time)
        .to(0.3, { position: cc.v3(px, 0, 0) })
        .call(() => {
          playerCard.push(node)
          this.emit(ListenEventType.PLAY_EFFECT, AudioType.card_dealing)
          cc.tween(node)
            .to(0.3, { scaleX: 0 })
            .call(() => {
              card.flopCard()
            })
            .to(0.3, { scaleX: 1 })
            .start()
          if (count < 4) {
            this.moveCard(playerCard, player)
          }
        }).start()
    }
    else {
      cc.tween(node)
        .delay(index * this.time)
        .to(0.3, { position: cc.v3(px, 0, 0) })
        .call(() => {
          playerCard.push(node)
          this.emit(ListenEventType.PLAY_EFFECT, AudioType.card_dealing)
          if (count < 4) {
            this.moveCard(playerCard, player)
          }
        }).start()
    }
  }

 // 发牌区发牌到底牌区域
 private dealCard2HoleCardArea(card: Card, player: CurrentPlayer, index: number, insertIndex: number) {
  let node = card.UICard.node
  let obj = this.getPlayerAreaByPlayerP(player)
  let playArea: cc.Node = obj.playArea
  let playerCard: cc.Node[] = obj.playerCard
  let holeCardArea: cc.Node = this.view['holeCardArea'];
  
  this.move(node, holeCardArea, false)
  let count = holeCardArea.childrenCount
  let px = 0
  if (player === CurrentPlayer.Hole) {
    // if (count >= 4) {
      px = 78
    // }
  }
  else {
    if (count >= 4) {
      px = (count - 4) * 48.4
    }
  }
  if (player === CurrentPlayer.Hole) {
    cc.tween(node)
      .delay(index * this.time)
      .to(0.3, { position: cc.v3(px, 0, 0) })
      .call(() => {
        // playerCard.push(node)
        // playerCard.unshift(node)
        // console.log('count', Math.round(count / 2))
        // let playerCardModel = getar
        // console.log('insertIndex = ', insertIndex)
        // console.log("card", card.color, card.index)
        this.holeCards.splice(insertIndex, 0, node)
        // playerCard.map((item, index) => {
        //   console.log(index, item.x)
        // })
        // this.getPlayerCards(player).map((item, index) => {

        
        this.emit(ListenEventType.PLAY_EFFECT, AudioType.card_dealing)
        cc.tween(node)
          .to(0.3, { scaleX: 0 })
          .call(() => {
            card.flopCard()
          })
          .to(0.3, { scaleX: 1 })
          .start()
        // if (count < 4) {
          
          this.moveCard(this.holeCards, player)
        // }
      }).start()
  }
  else {
    cc.tween(node)
      .delay(index * this.time)
      .to(0.3, { position: cc.v3(px, 0, 0) })
      .call(() => {
        playerCard.push(node)
        this.emit(ListenEventType.PLAY_EFFECT, AudioType.card_dealing)
        if (count < 4) {
          this.moveCard(playerCard, player)
        }
      }).start()
  }

}

  // 发牌区发牌到玩牌区(当手牌没合适牌时)
  public dealCardByPlay(card: Card, player: CurrentPlayer, index: number) {
    let node = card.UICard.node
    let obj = this.getPlayerAreaByPlayerP(player)
    let playArea: cc.Node = obj.playArea
    let playerCard: cc.Node[] = obj.playerCard
    this.move(node, playArea, false)
    let count = playArea.childrenCount
    let px = 0
    if (this.playerP === playerPeople.threeP) {
      if (player === CurrentPlayer.Player1) {
        px = 78
      } else {
        px = 48.4
        node.angle = 90
      }
    }
    else if (this.playerP === playerPeople.fourP) {
      if (player === CurrentPlayer.Player1 || player === CurrentPlayer.Player3) {
        px = 78
      } else {
        px = 48.4
        node.angle = 90
      }
    }
    else {
      px = 78
    }
    px = playArea.children[count - 2].position.x + px * (index + 1)
    cc.tween(node)
      .delay(index * 0.3)
      .to(0.3, { position: cc.v3(px, 0, 0) })
      .call(() => {
        playerCard.push(node)
        this.emit(ListenEventType.PLAY_EFFECT, AudioType.card_dealing)
        if (player === CurrentPlayer.Player1) {
          cc.tween(node)
            .to(0.3, { scaleX: 0 })
            .call(() => {
              card.flopCard()
              this.handleCardPos(playerCard, player)
            })
            .to(0.3, { scaleX: 1 })
            .start()
        }
        else {
          this.handleCardPos(playerCard, player)
        }
      })
      .start()
  }




  //通过玩家人数获取玩牌区域
  private getPlayerAreaByPlayerP(player: CurrentPlayer): any {
    let playArea: cc.Node = null
    let playerCard: cc.Node[] = []
    switch (this.playerP) {
      case playerPeople.twoP:
        switch (player) {
          case CurrentPlayer.Player1:
            playArea = this.view['player1/cardArea'];
            playerCard = this.player1PCards;
            break;
          case CurrentPlayer.Player2:
            playArea = this.view['player3/cardArea'];
            playerCard = this.player2PCards;
            break;
        };
        break;
      case playerPeople.threeP:
        switch (player) {
          case CurrentPlayer.Player1:
            playArea = this.view['player1/cardArea'];
            playerCard = this.player1PCards;
            break;
          case CurrentPlayer.Player2:
            playArea = this.view['player2/cardArea'];
            playerCard = this.player2PCards;
            break;
          case CurrentPlayer.Player3:
            playArea = this.view['player4/cardArea'];
            playerCard = this.player3PCards;
            break;
        };
        break;
      case playerPeople.fourP:
        switch (player) {
          case CurrentPlayer.Player1:
            playArea = this.view['player1/cardArea'];
            playerCard = this.player1PCards;
            break;
          case CurrentPlayer.Player2:
            playArea = this.view['player2/cardArea'];
            playerCard = this.player2PCards;
            break;
          case CurrentPlayer.Player3:
            playArea = this.view['player3/cardArea'];
            playerCard = this.player3PCards;
            break;
          case CurrentPlayer.Player4:
            playArea = this.view['player4/cardArea'];
            playerCard = this.player4PCards;
            break;
        };
        break;
    }

    return {
      playArea: playArea,
      playerCard: playerCard
    }
  }

  // 显示选择颜色面板
  public showSelectColorPanel() {
    this.show_ui_at(this.node, PanelType.POPUPPANEL, this, false, null, PopupType.selectColor)
  }

  // 设置UNO图标
  public setUNO(player: CurrentPlayer) {
    let allPlayer = this.returnAllPlayer()
    let playArea = allPlayer[player]
    let node = this.view[`${playArea}/uno`]
    node.active = true
    this.emit(ListenEventType.PLAY_EFFECT, AudioType.snap)
    cc.tween(node).to(0.3, { scale: 1.3 }).to(0.3, { scale: 1 }).call(() => { node.active = false }).start()
  }

  // 设置分数
  public setPoints(player: CurrentPlayer, points: number) {
    let allPlayer = this.returnAllPlayer()
    let playArea = allPlayer[player]
    // console.log('setPoints', playArea, points.toString())
    let node = this.view[`${playArea}/points`]
    node.active = true
    node.getComponent(cc.Label).string = points.toString()
    // this.emit(ListenEventType.PLAY_EFFECT, AudioType.snap)
    cc.tween(node).to(0.3, { scale: 1.3 }).to(0.3, { scale: 1 }).call(() => { }).start()
  }

  // 显示玩家手牌
  public showSuitableCards(cards: Card[]) {
    this.player1PCards.forEach(node => {
      // 设置节点透明度为50%
      // node.colodr = cc.Color.GRAY
      // 节点设置不可以点击

      // node.opacity = 100

      // node.active = false
    });

    cards.forEach(card => {
      card.UICard.node.color = cc.Color.GRAY
      if (card.canClick)
        card.UICard.node.color = cc.Color.WHITE
      // card.UICard.node.opacity = 100
      // card.UICard.node.active = true
    });
    // let node = card.UICard.node
    // let obj = this.getPlayerAreaByPlayerP(player)
    // let playArea: cc.Node = obj.playArea
    // let playerCard: cc.Node[] = obj.playerCard
    // this.move(node, playArea, false)
  }
  // 显示游戏结束面板
  public showGameoverPanel(isWin: boolean) {
    this.show_ui_at(this.node, PanelType.GAMEOVERPANEL, this, false, null, isWin)
  }
}
